<?php
require_once('include/sessionclass.php');
session_start();
require_once('libs/Smarty.class.php');
require_once('include/ngonngu.php');
require_once('include/oracle.php');
$smarty = new Smarty();
/****************************************************/
$smarty->template_dir = 'smarty/templates';
$smarty->compile_dir = 'smarty/templates_c';
$smarty->cache_dir = 'smarty/cache';
$smarty->config_dir = 'smarty/configs';
$javascript ='<link href="style/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
	if (document.images) {
		buttonb      = new Image();
		buttonb.src   = "images/first.gif" ;
	}
	if (document.images) {
		var home = top.document.forms[\'frmindex\'];
		if(home){
			var homeimg = home.homebutton;
		}
		
		if(homeimg){
			homeimg.src = buttonb.src;
		}
	}
	
</script>
';
$smarty->assign('javascript', $javascript);

$smarty->assign('lbl_tendangnhap', $VN_template[USERID]);
$smarty->assign('lbl_matkhau', $VN_template[USERPASS]);
$smarty->assign('ten_dangnhap', $VN_template[LOGIN]);

$log = $_GET['log'];
if(isset($log)){
	if($log=='logout'){
		session_unset();
		$host  = $_SERVER['HTTP_HOST'];
		$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
		redirect("http://$host$uri/login.php");		
	}
}

if(isset($_POST['dangnhap'])){
	$userid = $_REQUEST['userid'];
	$userpass = $_POST['userpass'];
	
	if(isset($userid) && isset($userpass)){
		//goi ham fun_checkUser
		/*--p_Msg: USRID|PASSWORD|*EOF|
		--Return: + "OK" : Dang nhap thanh cong
		----------+ "NO" : Dang nhap that bai
		*/
		if(isset($_SESSION['login'])){
			unset($_SESSION['login']);
		}
		$userpass = md5($userpass);
		$strquery = "$userid|$userpass|*EOF|";
		$verify = get_function(fun_checkUser,$strquery);
		if($verify=="NO"){
			$error=$VN_template[LOGINFAILD];
		}else{
			$login = new clogin();
			$login->userid = $userid;
			$login->userpass = $userpass;
			/*$login = new clogin();
			$login->userid = $userid;
			$login->userpass = $userpass;*/
			
			$_SESSION['login'] = serialize($login);
			
			if(isset($_SESSION['timeout'])){
				unset($_SESSION['timeout']);
			}
			$_SESSION['timeout'] = time();
			
			if(isset($_SESSION['login'])){
				$error = $login->userid . " " . $VN_template[LOGINOK];
				$host  = $_SERVER['HTTP_HOST'];
				$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
				redirect("http://$host$uri/maincontent.php");
			}
		}
		
		
	}else{
		session_unset();
	}
}


if(isset($error))
	$smarty->assign('error', $error);
else
	$smarty->assign('error', "");
$smarty->display('login.tpl');
?>